package com.service.impl;

import com.bean.Admin;
import com.bean.Class;
import com.dao.AdminDao;
import com.dao.impl.AdminDaoImpl;
import com.service.AdminService;
import com.utils.Page;
import com.utils.WebUtils;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    AdminDao adminDao = new AdminDaoImpl();

    @Override
    public int login(Admin record) {
        int status = existsUsername(record.getNo());
        if (status == 0) {
            Admin admin = adminDao.selectBy(record);
            if (admin != null) {
                WebUtils.copyBean(admin, record);
            } else {
                status = 2;
            }
        }
        return status;
    }

    @Override
    public int existsUsername(String username) {
        int status = 1;
        Admin admin = new Admin();
        admin.setNo(username);
        admin = adminDao.selectBy(admin);
        if (admin != null) {
            if (admin.getNo().equals(username)) {
                status = 0;
            }
        }
        return status;
    }

    @Override
    public int updatePassword(Integer id, String password, String newPassword) {
        int status = 0;
        Admin admin = new Admin();
        admin.setId(id);
        admin.setPassword(password);
        admin = adminDao.selectBy(admin);
        if (admin != null) {
            admin.setPassword(newPassword);
            adminDao.updateByPrimaryKey(admin);
        } else {
            status = 1;
        }
        return status;
    }

    @Override
    public int updeteInfo(Admin record) {
        int status = 0;
        if (adminDao.updateByPrimaryKey(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int insert(Admin record) {
        int status = 0;
        Admin admin = new Admin();
        admin.setName(record.getName());
        admin = adminDao.selectBy(admin);
        if (admin != null) {
            status = 1;
        } else if (adminDao.insert(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public Page<Admin> page(int pageNo, int pageSize, String content) {
        Page<Admin> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = adminDao.selectForPageTotalCount(content);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Admin> items = adminDao.selectForPageItems(begin, pageSize, content);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Admin selectBy(Admin admin) {
        return adminDao.selectBy(admin);
    }

    @Override
    public int deleteById(Integer id) {
        int status = 0;
        if (adminDao.deleteByPrimaryKey(id) == -1) {
            status = 1;
        }
        return status;
    }
}
